package com.premedsidekick;

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorTreeAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class Employment extends Activity {
	EmploymentDatabaseAdapter dbHelper;
	Cursor cursor;
	Button button;
	SharedPreferences preferences;
	Editor editor;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.employment);
		dbHelper = new EmploymentDatabaseAdapter(this);
		dbHelper.open();
		//dbHelper.createBlank("Alpha", "Alpha");
		//dbHelper.createBlank("Beta", "Beta");
		cursor = dbHelper.getAllIds();
		
		
		ExpandableListView listview = (ExpandableListView)findViewById(R.id.employment_expandable_listview);
		listview.setAdapter(new Test(cursor, this, dbHelper));
		listview.setGroupIndicator(null);
		
		button = (Button)findViewById(R.id.employment_add);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//System.out.println("Working");
				dbHelper.createBlank("Test", "Test");
				cursor.requery();
			}
			
		});
		preferences = getSharedPreferences("user_settings", 0);
		editor = preferences.edit();
		int usages = preferences.getInt("usages", 0);
		if (usages < 2) {
			final int state = usages + 1;
			Dialog dialog = new Dialog(this);
			dialog.setCancelable(true);
			dialog.setTitle("Basic Instructions");
			dialog.setContentView(R.layout.maindialog);
			TextView textview = (TextView)dialog.findViewById(R.id.TextView01);
			String message = getResources().getString(R.string.instructions);
			CharSequence sequence = Html.fromHtml(message);
			textview.setText(sequence);
			dialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					editor.putInt("usages", state);
					editor.commit();
				}
				
			});
			dialog.show();
		}
	}
	public class Test extends CursorTreeAdapter {
		EmploymentDatabaseAdapter dbHelper;
		Context context;
		Cursor cursor;
		Cursor row;
		View view;
		LayoutInflater inflater;
		Cursor child;
		
		public Test(Cursor cursor, Context context, EmploymentDatabaseAdapter dbHelper) {
			super(cursor, context);
			this.context = context;
			this.cursor = cursor;
			this.dbHelper = dbHelper;
			inflater = LayoutInflater.from(context);
		}
		@Override
		protected void bindChildView(View view, Context context, Cursor cursor,
				boolean isLastChild) {
			//System.out.println("bindChildView is called");
			view = inflater.inflate(R.layout.employment_expandable_listview_expanded, null);
			//System.out.println(cursor);
			int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
			String string = cursor.getString(id);
			//System.out.println("id: " + string);
			//int id = cursor.getColumnIndex(dbHelper.KEY_TITLE);
			child = dbHelper.getRow(Integer.parseInt(string));
			//System.out.println(child.getCount() + " hi");
			child.moveToFirst();
			TextView text = (TextView)view.findViewById(R.id.expandable_edittext_title);
			id = child.getColumnIndex(dbHelper.KEY_TITLE);
			string = child.getString(id);
			text.setText(string);
			
			text = (TextView)view.findViewById(R.id.expandable_edittext_company);
			id = child.getColumnIndex(dbHelper.KEY_COMPANY);
			string = child.getString(id);
			text.setText(string);
			
			
		}

		@Override
		protected void bindGroupView(View view, Context context, Cursor cursor,
				boolean isExpanded) {
			//System.out.println("bindGroupView is called");
			view = inflater.inflate(R.layout.expandable_listview_group_view, null);
			TextView textview = (TextView)view.findViewById(R.id.expandable_listview_group_view_textview);
			//cursor.getString(0);
			int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
			String sting = cursor.getString(id);
			//System.out.println("In group: " + sting);
			//System.out.println(sting);
			row = dbHelper.getRow(Integer.parseInt(sting));
			row.moveToFirst();
			id = row.getColumnIndex(dbHelper.KEY_TITLE);
			//System.out.println("Entry: " + id);
			String string = row.getString(id);
			//System.out.println("String: " + string);
			textview.setText(string);
			
		}

		@Override
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			//System.out.println("WTF");
			//System.out.println(groupCursor);
			return groupCursor; 
		}
		
		@Override
		public int getChildrenCount(int groupPosition) {
            return 1;
        }
		
		@Override
		protected View newChildView(Context context, Cursor cursor,
				boolean isLastChild, ViewGroup parent) {
			//System.out.println("newChildView is called");
			//Cursor test = dbHelper.getAll();
			//System.out.println("TOTAL COUNT: " + test.getCount());
			view = inflater.inflate(R.layout.employment_expandable_listview_expanded, null);
			//System.out.println(cursor);
			int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
			String string = cursor.getString(id);
			//System.out.println("id: " + string);
			//int id = cursor.getColumnIndex(dbHelper.KEY_TITLE);
			child = dbHelper.getRow(Integer.parseInt(string));
			//System.out.println(child.getCount() + " hi");
			child.moveToFirst();
			TextView text = (TextView)view.findViewById(R.id.expandable_edittext_title);
			id = child.getColumnIndex(dbHelper.KEY_TITLE);
			string = child.getString(id);
			text.setText(string);
			
			text = (TextView)view.findViewById(R.id.expandable_edittext_company);
			id = child.getColumnIndex(dbHelper.KEY_COMPANY);
			string = child.getString(id);
			text.setText(string);
			
			return view;
		}

		@Override
		protected View newGroupView(Context context, Cursor cursor,
				boolean isExpanded, ViewGroup parent) {
			//System.out.println("newGroupView is called");
			view = inflater.inflate(R.layout.expandable_listview_group_view, null);
			TextView textview = (TextView)view.findViewById(R.id.expandable_listview_group_view_textview);
			//cursor.getString(0);
			int id = cursor.getColumnIndex(dbHelper.KEY_ROWID);
			String sting = cursor.getString(id);
			//System.out.println("In group: " + sting);
			//System.out.println(sting);
			row = dbHelper.getRow(Integer.parseInt(sting));
			row.moveToFirst();
			id = row.getColumnIndex(dbHelper.KEY_TITLE);
			//System.out.println("Entry: " + id);
			String string = row.getString(id);
			//System.out.println("String: " + string);
			textview.setText(string);
			return view;
		}
		
	}
	/*
	public class ExpandableListCursorAdapter extends SimpleCursorTreeAdapter  {

		ExpanadableListCursorAdapter() {
			
		}
		/*
		private void bindView(View view, Context context, Cursor cursor, 
			int[] from, int[] to) {


		}
		
		@Override
		protected Cursor getChildrenCursor(Cursor arg0) {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		protected void bindChildView(View view, Context context, Cursor cursor, 
				boolean isLastChild) {

		//bindView(view, context, cursor, mChildFrom, mChildTo);
		}

		@Override
		protected void bindGroupView(View view, Context context, Cursor cursor, 
				boolean isExpanded) {

		//bindView(view, context, cursor, mGroupFrom, mGroupTo);
		}
		
	}
*/
	/*
	public class ExpandableListAdapter extends BaseExpandableListAdapter {
		Context context;
		Cursor cursor;
		EmploymentDatabaseAdapter dbHelper;
		LayoutInflater inflater;
		View view;
		
		ExpandableListAdapter(Context context, Cursor cursor, EmploymentDatabaseAdapter dbHelper) {
			this.context = context;
			this.cursor = cursor;
			this.dbHelper = dbHelper;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return "test";
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			
			return view;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public Object getGroup(int groupPosition) {
			// TODO Auto-generated method stub
			return "test";
		}

		@Override
		public int getGroupCount() {
			// TODO Auto-generated method stub
			return cursor.getCount();
			
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			//cursor = dbHelper.getAll();
			cursor.moveToNext();
			System.out.println(cursor.getCount());
			//if (cursor.getCount() == 0) 
			//	System.out.println("WTF");
			view = inflater.inflate(R.layout.expandable_listview_group_view, null);
			TextView textview = (TextView)view.findViewById(R.id.expandable_listview_group_view_textview);
			int id = cursor.getColumnIndex(dbHelper.KEY_TITLE);
			String title = cursor.getString(id);
			textview.setText(title);
			view.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View arg0) {
					// TODO Auto-generated method stub
					return true;
				}
				
			});
			
			return view;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return true;
		}
	}
*/
	public class EmploymentDatabaseAdapter {
		int DATABASE_VERSION = 1;
		String DATABASE_NAME = "employment_table";
		String KEY_ROWID = "_id";
		String KEY_TITLE = "title";
		String KEY_COMPANY = "company";
		String KEY_CITY = "city";
		String KEY_STATE = "state";
		String KEY_START_DATE = "start_date";
		String KEY_END_DATE = "end_date";
		String KEY_DUTY_ONE = "duty_one";
		String KEY_DUTY_TWO = "duty_two";
		String KEY_DUTY_THREE = "duty_three";
		String KEY_DUTY_FOUR = "duty_four";
		String KEY_DUTY_FIVE = "duty_five";
		String KEY_DUTY_SIX = "duty_six";
		
		String KEY_WAGE = "wage";
		
		EmploymentDatabaseHelper dbHelper;
		SQLiteDatabase database;
		ContentValues values;
		
		Context context;
		
		EmploymentDatabaseAdapter(Context context) {
			this.context = context;	
		}
		public EmploymentDatabaseAdapter open() throws SQLException {
			dbHelper = new EmploymentDatabaseHelper(context);
			database = dbHelper.getWritableDatabase();
			return this;
		}
		public void close() {
			dbHelper.close();
		}
		public long createBlank(String title, String company) {
			values = new ContentValues();
			values.put(KEY_TITLE, title);
			values.put(KEY_COMPANY, company);
			return database.insert(DATABASE_NAME, null, values);
		}
		public long createBlank() {
			values = new ContentValues();
			values.put(KEY_TITLE, "Click to Insert");
			values.put(KEY_COMPANY, "");
			values.put(KEY_CITY, "");
			values.put(KEY_STATE, "");
			values.put(KEY_START_DATE, "");
			values.put(KEY_END_DATE, "");
			values.put(KEY_DUTY_ONE, "");
			values.put(KEY_DUTY_TWO, "");
			values.put(KEY_DUTY_THREE, "");
			values.put(KEY_DUTY_FOUR, "");
			values.put(KEY_DUTY_FIVE, "");
			values.put(KEY_DUTY_SIX, "");
			return database.insert(DATABASE_NAME, null, values);
		}
		public boolean update(long rowId, String title, String company, String city, String state,
				String start_date, String end_date, String duty_one, String duty_two,
				String duty_three, String duty_four, String duty_five, String duty_six) {
			values = new ContentValues();
			values.put(KEY_TITLE, title);
			values.put(KEY_COMPANY, company);
			values.put(KEY_CITY, city);
			values.put(KEY_STATE, state);
			values.put(KEY_START_DATE, start_date);
			values.put(KEY_END_DATE, end_date);
			values.put(KEY_DUTY_ONE, duty_one);
			values.put(KEY_DUTY_TWO, duty_two);
			values.put(KEY_DUTY_THREE, duty_three);
			values.put(KEY_DUTY_FOUR, duty_four);
			values.put(KEY_DUTY_FIVE, duty_five);
			values.put(KEY_DUTY_SIX, duty_six);
			return database.update(duty_six, values, KEY_ROWID + "=" + rowId, null) > 0;
		}
		public boolean delete(long rowId) {
			return database.delete(DATABASE_NAME, KEY_ROWID + "=" + rowId, null) > 0;
		}
		public Cursor getTitle(long rowId) {
			Cursor cursor = database.query(DATABASE_NAME, new String[] {
					KEY_TITLE}, KEY_ROWID + "=" + rowId, null, null,
					null, null);
			return cursor;
		}
		public Cursor getRow(long rowId) {
			return database.query(DATABASE_NAME, new String[] {KEY_TITLE, KEY_COMPANY, 
					KEY_CITY, KEY_STATE, KEY_START_DATE,
					KEY_END_DATE, KEY_DUTY_ONE, KEY_DUTY_TWO, KEY_DUTY_THREE,
					KEY_DUTY_FOUR, KEY_DUTY_FIVE, KEY_DUTY_SIX}, KEY_ROWID + "=" + rowId, null, 
					null, null, null);
		}
		public Cursor getAllIds() {
			cursor = database.query(DATABASE_NAME, new String[] {KEY_ROWID}, null, null, null,
					null, null);
			return cursor;
		}
		public Cursor getAll() {
			Cursor cursor = database.query(DATABASE_NAME, new String[] {KEY_ROWID,
					KEY_TITLE, KEY_COMPANY, KEY_CITY, KEY_STATE, KEY_START_DATE,
					KEY_END_DATE, KEY_DUTY_ONE, KEY_DUTY_TWO, KEY_DUTY_THREE,
					KEY_DUTY_FOUR, KEY_DUTY_FIVE, KEY_DUTY_SIX}, null, null, null,
					null, null);
			if (cursor.getCount() == 0) {
				createBlank();
				createBlank();
				cursor = database.query(DATABASE_NAME, new String[] {KEY_ROWID,
						KEY_TITLE, KEY_COMPANY, KEY_CITY, KEY_STATE, KEY_START_DATE,
						KEY_END_DATE, KEY_DUTY_ONE, KEY_DUTY_TWO, KEY_DUTY_THREE,
						KEY_DUTY_FOUR, KEY_DUTY_FIVE, KEY_DUTY_SIX}, null, null, null,
						null, null);
			}
			return cursor;
		}
		public class EmploymentDatabaseHelper extends SQLiteOpenHelper {
			EmploymentDatabaseHelper(Context context) {
				super(context, DATABASE_NAME, null, DATABASE_VERSION);
			}

			@Override
			public void onCreate(SQLiteDatabase db) {
				db.execSQL("CREATE TABLE " + DATABASE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
					KEY_TITLE + " TEXT, " + KEY_COMPANY + " TEXT, " + KEY_CITY + " TEXT, " +
					KEY_STATE + " TEXT, " + KEY_START_DATE + " TEXT, " + KEY_END_DATE + " TEXT, " +
					KEY_DUTY_ONE + " TEXT, " + KEY_DUTY_TWO + " TEXT," + KEY_DUTY_THREE + " TEXT, " +
					KEY_DUTY_FOUR + " TEXT, " + KEY_DUTY_FIVE + " TEXT, " + KEY_DUTY_SIX + " TEXT);");
				
			}

			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion,
					int newVersion) {
				// TODO Auto-generated method stub
				
			}
		}
	}
}
